package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.City;
import com.ruoyi.system.mapper.CityMapper;
import com.ruoyi.system.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 城市 业务层处理
 *
 * @author ruoyi
 */
@Service
public class CityServiceImpl implements ICityService
{
    @Autowired
    private CityMapper cityMapper;

    /**
     * 查询城市数据集合
     *
     * @return 城市数据集合
     */
    @Override
    public List<City> selectCityList()
    {
        return cityMapper.selectCityList();
    }

    /**
     * 构建前端所需要树结构
     *
     * @param cities 城市列表
     * @return 树结构列表
     */
    @Override
    public List<City> buildCityTree(List<City> cities)
    {
        // This method is kept for compatibility but will not be used by the controller.
        // The new logic is in selectCityTree.
        return new ArrayList<>();
    }

    /**
     * 查询城市并构建树形结构
     * @return
     */
    @Override
    public List<City> selectCityTree() {
        List<City> allCities = selectCityList();
        // The parentid of root node (e.g., '中国') is usually 0 or null.
        List<City> cityTree = allCities.stream()
                .filter(city -> city.getParentid() == null || city.getParentid() == 0)
                .peek(city -> city.setChildren(findChildren(city, allCities)))
                .collect(Collectors.toList());
        return cityTree;
    }

    /**
     * Recursively finds children for a given root node from a list of all nodes.
     *
     * @param root      The root node to find children for.
     * @param allCities The list of all cities to search from.
     * @return A list of child nodes.
     */
    private List<City> findChildren(City root, List<City> allCities) {
        return allCities.stream()
                // Find nodes whose parentid matches the root's id.
                .filter(city -> root.getId().equals(city.getParentid()))
                // For each child, recursively find its own children.
                .peek(city -> city.setChildren(findChildren(city, allCities)))
                .collect(Collectors.toList());
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<City> list, City t)
    {
        // 得到子节点列表
        List<City> childList = getChildList(list, t);
        t.setChildren(childList);
        for (City tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<City> getChildList(List<City> list, City t)
    {
        List<City> tlist = new ArrayList<City>();
        for (City n : list)
        {
            if (n.getParentid() != null && n.getParentid().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<City> list, City t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 递归获取子节点
     * @param city
     * @param cities
     * @return
     */
    private List<City> getCityChildren(City city, List<City> cities) {
        List<City> children = cities.stream()
                .filter(c -> c.getParentid().equals(city.getId()))
                .peek(c -> c.setChildren(getCityChildren(c, cities)))
                .collect(Collectors.toList());
        return children;
    }
} 